package com.chery.media.ui.setting.language

import android.os.Bundle
import com.chery.media.R
import com.chery.media.databinding.FragmentLanguageBinding
import com.chery.media.library.base.BaseVMFragment
import com.chery.media.utils.LanguageSwitchUtils

class LanguageFragment: BaseVMFragment<LanguageViewModel, FragmentLanguageBinding>() {

    val languageAdapter by lazy {
        LanguageAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                setIndex(position)
                LanguageSwitchUtils.switchLanguage(position + 1)
                requireActivity().finish()
            }
        }
    }

    override val layoutId: Int
        get() = R.layout.fragment_language

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding.languageList.adapter = languageAdapter
        languageAdapter.setList(viewModel.language)
        if (viewModel.currentLanguage == 0) {
            languageAdapter.setIndex(1)
        } else {
            languageAdapter.setIndex(viewModel.currentLanguage - 1)
        }
    }

    companion object {
        fun newInstance() = LanguageFragment()
    }

}